<?php
class GroupsController extends Zend_Controller_Action
{
	protected $_acl;
	protected $_userGroup;
	
	public function init()
	{
		$auth = Zend_Auth::getInstance();
		$userInfo = $auth->getStorage()->read();
		$groupBase = new GroupBase();
		if ($userInfo) {
			$userGroups = $groupBase->getUserGroup($userInfo->name);
		}
		$this->_userGroup = $userGroups[0]['name'] ? $userGroups[0]['name'] : "Users";
		$this->_acl = new Acl_PPMIS();
		$this->view->acl = $this->_acl;
		$this->view->userGroup = $this->_userGroup;
	}
	
	public function preDispatch()
	{
		$auth = Zend_Auth::getInstance();
		if($auth->getIdentity()) {
			$this->view->auth = "success";
			$userInfo = $auth->getStorage()->read();
			$this->view->userInfo = $userInfo;
		}
		$Configuration = new Configuration();
		$configurationData = $Configuration->getConfigurationInfo();
		$this->view->configuration = $configurationData;
		$this->view->current = "admin";
		//$this->view->auth = 'Success';
	}
	
	public function browseAction()
	{
		$groupBase = new GroupBase();
		if ($_POST) {
			if ($_POST['groupname']) {
				$groupBase->insert(array('groupname'=>$_POST['groupname']));
			}
		}
		$allGroups = $groupBase->getAllGroups();
		$i = 0;
		foreach ($allGroups as $group) {
			$allGroups[$i]['users'] = $groupBase->getGroupMembersCount($group['id']);
			$i++;
		}
		
		$this->view->groups = $allGroups;
	}
	
	public function deleteAction()
	{
		$id = $this->_getParam('id');
		
		$groupBase = new GroupBase();
		//$groupBase->delete("id = $id");
		$this->_redirect('groups/browse');
	}
}